﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Foxit.PDF;
using Foxit.PDF.PageElements;
using Sustainalytics.Reporting.Framework;

namespace Sustainalytics.ReportGenerator.BL
{
    public class SourcesSection : IPdfDocumentGenerator
    {
        private CompanyReportDataOffline _dataSource;
        private PdfReport _report;


        public GenerationResult GeneratePdfDocument(CompanyReportDataOffline crdo)
        {
            var result = new GenerationResult
            {
                ReportType = crdo.ReportType,
                ReportGeneratorType = ReportGeneratorType.SourcesSection,
                OutlinePositions = new List<OutlinePosition>(),
                PageElementPositions = new List<PageElementPosition>()
            };
            var sw = Stopwatch.StartNew();

            _dataSource = crdo;
            _report = new PdfReport();

            ComposeSourcesSection();

            if (_report.Pages.Any())
                result.PdfDocument = _report.Draw();

            sw.Stop();
            result.GenerationTime = sw.Elapsed;

            return result;
        }

        private void ComposeSourcesSection()
        {
            var page = _report.NewPage();
            _report.Pages.Add(page);

            var strBld = new StringBuilder();

            float x = _report.LeftMargin;
            float y = 0;

            page.Elements.Add(new Rectangle(x, y, 535, 25, Colors.BarBackgroundColor, Colors.BarBackgroundColor));
            page.Elements.Add(new FormattedTextArea("<p><font color='FFFFFF'><b>Sources</b></font></p>", x + 5f, y + 5f, 500, 16, FontFamily.Times, 16, false));
            page.Elements.Add(new Line(x, y + 30, 545, y + 30, 1f, RgbColor.Black));

            y += 40;

            strBld.Append("<p>All data in this report is based on information found in the following sources.</p>");
            strBld.Append("<p>Events have their sources referenced where they appear in the report.</p>");

            page.Elements.Add(new FormattedTextArea(strBld.ToString(), x, y + 5f, 545, 30, Fonts.CalibriFontFamily, 9, false));
            strBld.Clear();

            y += 35;

            page.Elements.Add(new FormattedTextArea("<p><b>Source Name</b></p>", x, y + 5f, 545, 16, Fonts.CalibriFontFamily, 11, false));

            y += 15;

            var sources = _dataSource.Sources;

            if (null == sources) return;

            var strBldSourceText = new StringBuilder();
            var strBldSourceIndNrs = new StringBuilder();
            foreach (var source in sources)
            {
                strBldSourceText.Append("<p>");
                strBldSourceText.Append(source.SourceText);
                strBldSourceText.Append("</p>");

                strBldSourceIndNrs.Append("<p>");
                strBldSourceIndNrs.Append(string.Join(", ", source.IndicatorsNumbers));
                strBldSourceIndNrs.Append("</p>");

                var formTxtAreaSourceText = new FormattedTextArea(strBldSourceText.ToString(), x, y + 5f, 370, 10, Fonts.CalibriFontFamily, 9, false);
                var reqHeightSourceText = formTxtAreaSourceText.GetRequiredHeight();

                var formTxtAreaSourceIndNrs = new FormattedTextArea(strBldSourceIndNrs.ToString(), x + 365, y + 5f, 150, 10, Fonts.CalibriFontFamily, 6, false);
                var reqHeightSourceIndNrs = formTxtAreaSourceIndNrs.GetRequiredHeight();

                var reqHeight = Math.Max(reqHeightSourceText, reqHeightSourceIndNrs);

                if (y + reqHeight > 770)
                {
                    page = _report.NewPage();
                    _report.Pages.Add(page);
                    y = 0;
                }

                formTxtAreaSourceText = new FormattedTextArea(strBldSourceText.ToString(), x, y + 5f, 370, reqHeight, Fonts.CalibriFontFamily, 9, false);
                page.Elements.Add(formTxtAreaSourceText);

                formTxtAreaSourceIndNrs = new FormattedTextArea(strBldSourceIndNrs.ToString(), x + 365, y + 5f, 150, reqHeight, Fonts.CalibriFontFamily, 6, false);
                formTxtAreaSourceIndNrs.Style.Paragraph.Align = TextAlign.Right;
                page.Elements.Add(formTxtAreaSourceIndNrs);

                y += reqHeight + 5;

                strBldSourceText.Clear();
                strBldSourceIndNrs.Clear();
            }
        }
    }
}
